package com.zr.userrepay.controller;

import com.zr.meiju.OpenBankEnum;
import com.zr.userrepay.pojo.UserRepayAddVo;
import com.zr.userrepay.pojo.XiaLaVo;
import com.zr.userrepay.service.UserRepayService;
import com.zr.util.LoginSession;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by star on 2019/7/3.
 */
@RestController
public class UserRepayController {

    /**
     * 新增还款银行卡登记 后面进行查询操作 根据loginid查询  如果没有信息就是新增  否则就是修改
     */
    @Autowired
    private UserRepayService userRepayService;
    @PostMapping("userRepay/add")
    public ResultVo addUserRepay(@RequestBody @Valid UserRepayAddVo userRepayAddVo, BindingResult bindingResult,HttpServletRequest request){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        Integer loginId= LoginSession.getLoginId(request.getSession());
        if(loginId==null){
            return ResultBuildVo.error("请重新登陆","500");
        }
        userRepayAddVo.setLoginId(loginId);
        return userRepayService.addUserRepay(userRepayAddVo,request);
    }


    /**s
     * 更据注册表loginId查看还款银行卡登记
     */
    @GetMapping("userRepay/findByLoginId")
    public ResultVo findByLoginId(HttpServletRequest request){
        Integer loginId = LoginSession.getLoginId(request.getSession());
        return  userRepayService.findByLoginId(loginId);
    }

    /**
     * 开户行下拉框
     */
    @GetMapping("kaihuhangDropdownBox")
    public ResultVo<List<XiaLaVo>> queryXiaLa(){
        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (int i = 0; i< OpenBankEnum.values().length; i++){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setId(i);
            xiaLaVo.setName(OpenBankEnum.getName(i));
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultBuildVo.success(xiaLaVoList);
    }




}
